package com.group02.community.controller;



import com.group02.community.entity.user.SysMenu;
import com.group02.community.entity.vo.MenuVO;
import com.group02.community.entity.vo.queryVO.MenuQueryVO;
import com.group02.community.service.IMenuService;
import com.group02.community.utils.MenuTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("menu")
public class MenuController {

    @Autowired
    IMenuService menuService;

    @PostMapping("menusQue")
    public ResponseEntity<List<MenuVO>> getAllMenu(MenuQueryVO mq){
        List<MenuVO> menu = menuService.getAllMenu(mq);
        return ResponseEntity.ok(menu);
    }

    @PostMapping("menus")
    public ResponseEntity<List<MenuVO>> selectMenuByQuery(MenuQueryVO mq){
        List<MenuVO> menu = menuService.selectMenuByQuery(mq);

        List<MenuVO> menuVOS = new MenuTree(menu).buildTree();
        return ResponseEntity.ok(menuVOS);
    }

    @PostMapping("menu")
    public ResponseEntity<String> addMenu(SysMenu menu){
        menuService.save(menu);
        return ResponseEntity.ok("添加成功");
    }

    @PutMapping("menu")
    public ResponseEntity<String> updateMenu(@RequestBody MenuVO menu){
        menuService.updateById(menu);
        return ResponseEntity.ok("修改成功");
    }


    @DeleteMapping("menu")
    public ResponseEntity<String> deleteMenu(Long id){
        menuService.removeById(id);
        return ResponseEntity.ok("删除成功");
    }

}
